package com.sicpa.common.utils;

import java.security.SecureRandom;
import java.util.Random;

/**
 * RandomNumCode
 *
 * @author lwf
 */
public class RandomNumCode {

    private static String codeStr = "0123456789";

    private static String letterStr = "ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz";

    private static String customStr = "ABCDEFGHJKLMNPQRSTUVWXYZ";

    private static int codeNum = 6;

    /**
     * 生成16位不重复的随机数，含数字+大小写
     *
     * @return
     */
    public static String getGUID() {
        StringBuilder uid = new StringBuilder();
        //产生16位的强随机数
        Random rd = new SecureRandom();
        for (int i = 0; i < 16; i++) {
            //产生0-2的3位随机数
            int type = rd.nextInt(3);
            switch (type) {
                case 0:
                    //0-9的随机数
                    uid.append(rd.nextInt(10));
                    break;
                case 1:
                    //ASCII在65-90之间为大写,获取大写随机
                    uid.append((char) (rd.nextInt(25) + 65));
                    break;
                case 2:
                    //ASCII在97-122之间为小写，获取小写随机
                    uid.append((char) (rd.nextInt(25) + 97));
                    break;
                default:
                    break;
            }
        }
        return uid.toString();
    }

    /**
     * 生成6位验证码
     *
     * @return
     */
    public static String generateNumCode() {
        return generateSpecificCode(codeNum);
    }

    /**
     * 生成随机码，字母加数字
     * @param length
     * @return
     */
    public static String getStringRandom(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        //参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {
            if (random.nextInt(2) % 2 == 0) {
                sb.append(letterStr.charAt(random.nextInt(letterStr.length())));
            }else{
                sb.append(random.nextInt(10));
            }
        }
        return sb.toString();
    }

    /**
     * 20201110，wym
     * 生成指定位数的纯数字验证码
     *
     * @return
     */
    public static String generateSpecificCode(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(codeStr.charAt(new Random().nextInt(10)));
        }
        return sb.toString();
    }

    /**
     * 20201110，wym
     * 生成指定位数的纯英文验证码，不包含英文 I 和 O
     *
     * @return
     */
    public static String getSpecificString(int length){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(customStr.charAt(new Random().nextInt(24)));
        }
        return sb.toString();
    }

    /*public static void main(String[] args) {
        System.out.println(new Random().nextInt(24));
        System.out.println(customStr.charAt(new Random().nextInt(24)));
    }*/


}
